package com.qffx.moviebase.adapter.find;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qffx.moviebase.R;
import com.qffx.moviebase.bean.find.FindNewestEntity;
import com.qffx.moviebase.view.FindMovieActivity;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lx on 2017/2/8.
 */

public class FindNewestAdapter extends RecyclerView.Adapter<FindNewestAdapter.ViewHolder> {

    private Context context;
    private List<FindNewestEntity.RecommendsListBean> listBeen;

    public FindNewestAdapter(List<FindNewestEntity.RecommendsListBean> listBeen, Context context) {
        this.listBeen = listBeen;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.find_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final FindNewestEntity.RecommendsListBean bean = listBeen.get(position);
        //加载头像
        //Picasso.with(context).load(bean.getWriterInfo().getHeadImgUrl()).transform(new CircleBitmapUtils()).error(R.drawable.test_bg).into(holder.head);
        holder.name.setText(bean.getWriterInfo().getNickname());
        holder.content.setText(bean.getRecommend());

        Picasso.with(context).load(bean.getFilmResources().getImages().getSmall()).error(R.drawable.iv_movie_default_still_night).into(holder.picture);
        holder.movieName.setText(bean.getFilmResources().getTitle());
        if(listBeen.get(position).getTag()){
            holder.follow.setSelected(true);
        }
        if(!listBeen.get(position).getTag()) {
            holder.follow.setSelected(false);
        }

        holder.actorName.setText(loadActorName(bean));
        holder.biaoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        holder.follow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if(b){
                  listBeen.get(position).setTag(true);
              }
                else {
                  listBeen.get(position).setTag(false);
              }

            }
        });
        if (bean.getPraiseCount() != 0) {
            holder.count.setText(bean.getPraiseCount() + "");
        } else {
            holder.count.setText("");
        }
        int createDate = bean.getCreateDate();
        Date date=new Date(createDate);
        SimpleDateFormat format= new SimpleDateFormat("MM-dd hh:mm");
        String format1 = format.format(date);
        holder.time.setText(format1);
        holder.movie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, FindMovieActivity.class);
                intent.putExtra("id",bean.getFilmResources().getId());
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return listBeen.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        //头像
        private ImageView head;
        //姓名
        private TextView name;
        //关注数量
        private TextView count;
        //点关注图标
        private CheckBox follow;
        //内容
        private TextView content;
        //电影图片
        private ImageView picture;
        //电影名
        private TextView movieName;
        //演员表
        private TextView actorName;
        //标记
        private ImageView biaoji;
        //时间
        private TextView time;
        //电影
        private RelativeLayout movie;
        public ViewHolder(View itemView) {
            super(itemView);
            head = (ImageView) itemView.findViewById(R.id.find_item_head);
            name = (TextView) itemView.findViewById(R.id.find_item_name);
            count = (TextView) itemView.findViewById(R.id.find_item_count);
            follow = (CheckBox) itemView.findViewById(R.id.find_item_follow);
            content = (TextView) itemView.findViewById(R.id.find_item_content);
            picture = (ImageView) itemView.findViewById(R.id.find_item_picture);
            movieName = (TextView) itemView.findViewById(R.id.find_item_moviename);
            actorName = (TextView) itemView.findViewById(R.id.find_item_actorname);
            biaoji = (ImageView) itemView.findViewById(R.id.find_item_biaoji);
            time = (TextView) itemView.findViewById(R.id.find_item_time);
            movie= (RelativeLayout) itemView.findViewById(R.id.find_movie);

        }
    }

    ////演员名字拼接字符串
    private StringBuffer loadActorName(FindNewestEntity.RecommendsListBean bean) {
        StringBuffer s = new StringBuffer();
        s.append("主演：");
        for (int i = 0; i < bean.getFilmResources().getCasts().size(); i++) {
            if (i == 0)
                s.append(bean.getFilmResources().getCasts().get(i).getName());
            else
                s.append("/" + bean.getFilmResources().getCasts().get(i).getName());

        }
        return s;
    }
}
